#!/usr/bin/python3
# -*- coding: utf-8 -*-
import datetime
import logging

import pytest


logger = logging.getLogger()
@pytest.fixture(scope="session")
def database():
    aa = 1
    yield aa
    b = aa


# conftest.py
def pytest_collection_modifyitems(items):
    # 反转测试顺序
    items[:] = items[::-1]

def pytest_unconfigure(config):
    print("清理全局资源...")


def pytest_terminal_summary(terminalreporter):
    passed = len(terminalreporter.stats.get("passed", []))
    failed = len(terminalreporter.stats.get("failed", []))

    print(f"\n通过率: {passed/(passed+failed)*100:.2f}%")

def pytest_generate_tests(metafunc):
    return metafunc

def pytest_sessionstart(session):
    print("测试会话开始，初始化数据库连接...")

def pytest_sessionfinish(session):
    print("测试会话结束，生成HTML报告...")
    session.config.option.htmlpath = r"E:\workspace\pywork\ptest\testcases\report.html"


def pytest_runtest_makereport(item, call):
    if call.when == "call" and call.excinfo:
        test_name = item.name
        error = call.excinfo.value
        print(f"测试失败: {test_name}, 错误: {error}")

def pytest_runtest_teardown(item):
    print(f"测试 {item.name} 执行完毕，清理资源...")

def pytest_runtest_setup(item):
    logger.error(item.keywords)
    if "skip" in item.keywords:
        pytest.skip("主动跳过此测试")

def pytest_configure(config):
    config.addinivalue_line(
        "markers",
        "slow: 标记为耗时测试用例"
    )